using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 格子
/// </summary>
public class Cell : MonoBehaviour
{
    [SerializeField]
    private Plant m_Plant;

    [SerializeField]
    private SpriteRenderer m_Renderer;

    [Header("行")]
    public int Row;

    [Header("列")]
    public int Column;

    private void Awake()
    {
        m_Renderer = transform.GetComponent<SpriteRenderer>();
    }



    /// <summary>
    /// 添加植物
    /// </summary>
    /// <param name="plant"></param>
    /// <returns></returns>
    public bool AddPlant(Plant plant)
    {
        if (m_Plant!=null)
        {
            return false;
        }

        var obj = GameObject.Instantiate<Plant>(plant,this.transform);
        obj.Initialize();
        m_Plant = obj;
        m_Plant.transform.position = transform.position;
        
        // 所有植物都先进入Idle状态
        // Jalapeno会在Idle状态中自动触发攻击
        m_Plant.FSMComponent.ChangeStatus(FSMType.Idle);
        
        return true;
    }

    private void OnMouseDown()
    {
        Logger.Log("Cell", $"OnMouseDown: Broadcasting ADD_PLANT event for cell at row {Row}, col {Column}");
        var args = ReferencePool.Acquire<AddPlantEventArgs>();
        // Note: Plant will be set by the handler
        EventHelper.Fire(this, args);
        Logger.Log("Cell", "OnMouseDown: Event broadcast completed");
    }

    private void OnMouseEnter()
    {
        //红色
        ColorUtility.TryParseHtmlString("#E70A0A", out Color color);
        m_Renderer.color = color;
    }

    private void OnMouseExit()
    {
        ColorUtility.TryParseHtmlString("#FFFFFF", out Color color);
        m_Renderer.color = color;
    }


}